package com.iraeta.controller.basedata;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.iraeta.common.ServiceException;
import com.iraeta.controller.BaseController;
import com.iraeta.entity.Ordermx;
import com.iraeta.entity.vo.OrdermxVo;
import com.iraeta.service.basedata.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController(value = "OrderController")
@RequestMapping("order")
public class OrderController extends BaseController {
    @Autowired
    OrderService orderService;

    @RequestMapping("pageordermx")
    public String pageOrderMx(@RequestBody JSONObject jo){
        String kehuName = jo.getString("kehuName");
        String orderNumber = jo.getString("orderNumber");
        this.pageSet(jo);
        List<OrdermxVo> ordermxVoList = orderService.listOrdermxVo(kehuName, orderNumber);
        PageInfo<OrdermxVo> ordermxVoPageInfo = this.getPageInfo(ordermxVoList);
        return this.success(ordermxVoPageInfo);
    }

    @RequestMapping("getordermx")
    public String getOrderMx(@RequestBody JSONObject paramJb){
        int orderMxId = paramJb.getIntValue("id");
        Ordermx ordermx = orderService.getOrdermx(orderMxId);
        return this.success(ordermx);
    }

    @RequestMapping("addeditordermx")
    public String addEditOrderMx(@RequestBody JSONObject paramJb){
        try {
            int orderMxId = paramJb.getIntValue("id");
            Ordermx ordermx = orderMxId <= 0 ? new Ordermx() :  orderService.getOrdermx(orderMxId);
            this.mapJsonToObj(paramJb, ordermx);
            if(orderMxId <= 0) ordermx.setId(0);
            if(orderMxId <= 0){
                ordermx.setMainid(0);
                orderService.addOrdermx(ordermx);
            }else {
                orderService.updateOrdermx(ordermx);
            }
        }catch (Exception e){
            throw new ServiceException(e.getMessage());
        }
        return this.success();
    }

    @RequestMapping("deleteordermx")
    public String deleteOrderMx(@RequestBody JSONObject paramJb){
        int orderMxId = paramJb.getIntValue("id");
        orderService.deleteOrdermx(orderMxId);
        return this.success();
    }

    @RequestMapping("pagekehuordermx")
    public String pageKeHuOrderMx(@RequestBody JSONObject jo){
        int kehuId = this.getUserId();
        if(kehuId <= 0){
            throw new ServiceException("Illegal access");
        }
        String orderNumber = jo.getString("orderNumber");
        this.pageSet(jo);
        List<OrdermxVo> ordermxVoList = orderService.listUserOrdermxVo(kehuId, orderNumber);
        PageInfo<OrdermxVo> ordermxVoPageInfo = this.getPageInfo(ordermxVoList);
        return this.success(ordermxVoPageInfo);
    }
}
